# Creating a control for picking a date
# https://dash.plotly.com/dash-core-components/datepickersingle
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=date(1995, 8, 5),
max_date_allowed=date(2017, 9, 19),
initial_visible_month=date(2017, 8, 5),
date=date(2017, 8, 25)
),
html.Div(id='output-container-date-picker-single')
])
@app.callback(
Output('output-container-date-picker-single', 'children'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
#date.fromisoformat(date_string)
#Return a date corresponding to a date_string given in the format YYYY-MM-DD:
date_object = date.fromisoformat(date_value)
date_string = date_object.strftime('%B %d, %Y')
return string_prefix + date_string
if __name__ == '__main__':
app.run_server(mode='inline', port=7865)
# Creating a control for picking a date
# https://strftime.org/
# https://dash.plotly.com/dash-core-components/datepickersingle
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=date(1995, 8, 5),
max_date_allowed=date(2017, 9, 19),
initial_visible_month=date(2017, 8, 5),
date=date(2017, 8, 25)
),
html.Div(id='output-container-date-picker-single')
])
@app.callback(
Output('output-container-date-picker-single', 'children'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
#date.fromisoformat(date_string)
#Return a date corresponding to a date_string given in the format YYYY-MM-DD:
date_object = date.fromisoformat(date_value)
#date_string = date_object.strftime('%Y-%m-%d')
date_string = date_object.isoformat()
return string_prefix + date_string
if __name__ == '__main__':
app.run_server(mode='inline', port=8833)
# Filtering a dataset based on date range
import pandas as pd
from datetime import date
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
#df.head()
df_filtered = df[df['Order Date'].isin(pd.date_range("2018-01-01", "2018-06-30"))]
df_filtered.head(20)
| Category | City | Country/Region | Order Date | Postal Code | FIPS | Region | Segment | State | State-Code | Sub-Category | Discount | Profit | Quantity | Sales | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5 | Furniture | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Furnishings | 0.0 | 14.1694 | 7 | 48.860 |
| 6 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Art | 0.0 | 1.9656 | 4 | 7.280 |
| 7 | Technology | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Phones | 0.2 | 90.7152 | 6 | 907.152 |
| 8 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Binders | 0.2 | 5.7825 | 3 | 18.504 |
| 9 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Appliances | 0.0 | 34.4700 | 5 | 114.900 |
| 10 | Furniture | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Tables | 0.2 | 85.3092 | 9 | 1706.184 |
| 11 | Technology | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Phones | 0.2 | 68.3568 | 4 | 911.424 |
| 17 | Office Supplies | West Jordan | United States | 2018-05-13 | 84084.0 | 49035.0 | West | Consumer | Utah | UT | Storage | 0.0 | 9.9900 | 2 | 55.500 |
| 157 | Furniture | Seattle | United States | 2018-03-01 | 98115.0 | 53033.0 | West | Consumer | Washington | WA | Chairs | 0.2 | 51.4764 | 2 | 457.568 |
| 195 | Office Supplies | Hamilton | United States | 2018-03-21 | 45011.0 | 39017.0 | East | Consumer | Ohio | OH | Art | 0.2 | 1.2038 | 2 | 7.408 |
| 196 | Office Supplies | Hamilton | United States | 2018-03-21 | 45011.0 | 39017.0 | East | Consumer | Ohio | OH | Art | 0.2 | 1.5876 | 3 | 6.048 |
| 244 | Furniture | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Chairs | 0.0 | 580.5394 | 7 | 2001.860 |
| 245 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Storage | 0.0 | 41.6800 | 2 | 166.720 |
| 246 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Paper | 0.0 | 23.9400 | 6 | 47.880 |
| 247 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Appliances | 0.0 | 496.0725 | 5 | 1503.250 |
| 248 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Paper | 0.0 | 12.4416 | 4 | 25.920 |
| 306 | Office Supplies | Lakewood | United States | 2018-06-01 | 8701.0 | 34029.0 | East | Home Office | New Jersey | NJ | Binders | 0.0 | 20.9208 | 3 | 45.480 |
| 307 | Office Supplies | Lakewood | United States | 2018-06-01 | 8701.0 | 34029.0 | East | Home Office | New Jersey | NJ | Art | 0.0 | 83.8680 | 6 | 289.200 |
| 340 | Office Supplies | Philadelphia | United States | 2018-04-13 | 19140.0 | 42101.0 | East | Corporate | Pennsylvania | PA | Art | 0.2 | 1.1160 | 4 | 17.856 |
| 341 | Office Supplies | Philadelphia | United States | 2018-04-13 | 19140.0 | 42101.0 | East | Corporate | Pennsylvania | PA | Binders | 0.7 | -407.9760 | 10 | 509.970 |
# Filtering a dataset based on date range using date objects
import pandas as pd
from datetime import date
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
start_date = date(2018, 1, 1)
end_date = date(2018, 6, 30)
df_filtered = df[df['Order Date'].isin(pd.date_range(start_date, end_date))]
df_filtered.head(20)
| Category | City | Country/Region | Order Date | Postal Code | FIPS | Region | Segment | State | State-Code | Sub-Category | Discount | Profit | Quantity | Sales | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5 | Furniture | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Furnishings | 0.0 | 14.1694 | 7 | 48.860 |
| 6 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Art | 0.0 | 1.9656 | 4 | 7.280 |
| 7 | Technology | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Phones | 0.2 | 90.7152 | 6 | 907.152 |
| 8 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Binders | 0.2 | 5.7825 | 3 | 18.504 |
| 9 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Appliances | 0.0 | 34.4700 | 5 | 114.900 |
| 10 | Furniture | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Tables | 0.2 | 85.3092 | 9 | 1706.184 |
| 11 | Technology | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Phones | 0.2 | 68.3568 | 4 | 911.424 |
| 17 | Office Supplies | West Jordan | United States | 2018-05-13 | 84084.0 | 49035.0 | West | Consumer | Utah | UT | Storage | 0.0 | 9.9900 | 2 | 55.500 |
| 157 | Furniture | Seattle | United States | 2018-03-01 | 98115.0 | 53033.0 | West | Consumer | Washington | WA | Chairs | 0.2 | 51.4764 | 2 | 457.568 |
| 195 | Office Supplies | Hamilton | United States | 2018-03-21 | 45011.0 | 39017.0 | East | Consumer | Ohio | OH | Art | 0.2 | 1.2038 | 2 | 7.408 |
| 196 | Office Supplies | Hamilton | United States | 2018-03-21 | 45011.0 | 39017.0 | East | Consumer | Ohio | OH | Art | 0.2 | 1.5876 | 3 | 6.048 |
| 244 | Furniture | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Chairs | 0.0 | 580.5394 | 7 | 2001.860 |
| 245 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Storage | 0.0 | 41.6800 | 2 | 166.720 |
| 246 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Paper | 0.0 | 23.9400 | 6 | 47.880 |
| 247 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Appliances | 0.0 | 496.0725 | 5 | 1503.250 |
| 248 | Office Supplies | Lakeville | United States | 2018-06-01 | 55044.0 | 27037.0 | Central | Home Office | Minnesota | MN | Paper | 0.0 | 12.4416 | 4 | 25.920 |
| 306 | Office Supplies | Lakewood | United States | 2018-06-01 | 8701.0 | 34029.0 | East | Home Office | New Jersey | NJ | Binders | 0.0 | 20.9208 | 3 | 45.480 |
| 307 | Office Supplies | Lakewood | United States | 2018-06-01 | 8701.0 | 34029.0 | East | Home Office | New Jersey | NJ | Art | 0.0 | 83.8680 | 6 | 289.200 |
| 340 | Office Supplies | Philadelphia | United States | 2018-04-13 | 19140.0 | 42101.0 | East | Corporate | Pennsylvania | PA | Art | 0.2 | 1.1160 | 4 | 17.856 |
| 341 | Office Supplies | Philadelphia | United States | 2018-04-13 | 19140.0 | 42101.0 | East | Corporate | Pennsylvania | PA | Binders | 0.7 | -407.9760 | 10 | 509.970 |
# Filtering a dataset based on a date range using today's date
import pandas as pd
from datetime import date
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
start_date = date(2018, 1, 1)
end_date = date.today()
df_filtered = df[df['Order Date'].isin(pd.date_range(start_date, end_date))]
df_filtered.head(20)
| Category | City | Country/Region | Order Date | Postal Code | FIPS | Region | Segment | State | State-Code | Sub-Category | Discount | Profit | Quantity | Sales | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Furniture | Henderson | United States | 2020-11-08 | 42420.0 | 21059.0 | South | Consumer | Kentucky | KY | Bookcases | 0.00 | 41.9136 | 2 | 261.9600 |
| 1 | Furniture | Henderson | United States | 2020-11-08 | 42420.0 | 21059.0 | South | Consumer | Kentucky | KY | Chairs | 0.00 | 219.5820 | 3 | 731.9400 |
| 2 | Office Supplies | Los Angeles | United States | 2020-06-12 | 90036.0 | 6037.0 | West | Corporate | California | CA | Labels | 0.00 | 6.8714 | 2 | 14.6200 |
| 3 | Furniture | Fort Lauderdale | United States | 2019-10-11 | 33311.0 | 12011.0 | South | Consumer | Florida | FL | Tables | 0.45 | -383.0310 | 5 | 957.5775 |
| 4 | Office Supplies | Fort Lauderdale | United States | 2019-10-11 | 33311.0 | 12011.0 | South | Consumer | Florida | FL | Storage | 0.20 | 2.5164 | 2 | 22.3680 |
| 5 | Furniture | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Furnishings | 0.00 | 14.1694 | 7 | 48.8600 |
| 6 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Art | 0.00 | 1.9656 | 4 | 7.2800 |
| 7 | Technology | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Phones | 0.20 | 90.7152 | 6 | 907.1520 |
| 8 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Binders | 0.20 | 5.7825 | 3 | 18.5040 |
| 9 | Office Supplies | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Appliances | 0.00 | 34.4700 | 5 | 114.9000 |
| 10 | Furniture | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Tables | 0.20 | 85.3092 | 9 | 1706.1840 |
| 11 | Technology | Los Angeles | United States | 2018-06-09 | 90032.0 | 6037.0 | West | Consumer | California | CA | Phones | 0.20 | 68.3568 | 4 | 911.4240 |
| 12 | Office Supplies | Concord | United States | 2021-04-15 | 28027.0 | 37025.0 | South | Consumer | North Carolina | NC | Paper | 0.20 | 5.4432 | 3 | 15.5520 |
| 13 | Office Supplies | Seattle | United States | 2020-12-05 | 98103.0 | 53033.0 | West | Consumer | Washington | WA | Binders | 0.20 | 132.5922 | 3 | 407.9760 |
| 14 | Office Supplies | Fort Worth | United States | 2019-11-22 | 76106.0 | 48439.0 | Central | Home Office | Texas | TX | Appliances | 0.80 | -123.8580 | 5 | 68.8100 |
| 15 | Office Supplies | Fort Worth | United States | 2019-11-22 | 76106.0 | 48439.0 | Central | Home Office | Texas | TX | Binders | 0.80 | -3.8160 | 3 | 2.5440 |
| 16 | Office Supplies | Madison | United States | 2018-11-11 | 53711.0 | 55025.0 | Central | Consumer | Wisconsin | WI | Storage | 0.00 | 13.3176 | 6 | 665.8800 |
| 17 | Office Supplies | West Jordan | United States | 2018-05-13 | 84084.0 | 49035.0 | West | Consumer | Utah | UT | Storage | 0.00 | 9.9900 | 2 | 55.5000 |
| 18 | Office Supplies | San Francisco | United States | 2018-08-27 | 94109.0 | 6075.0 | West | Consumer | California | CA | Art | 0.00 | 2.4824 | 2 | 8.5600 |
| 19 | Technology | San Francisco | United States | 2018-08-27 | 94109.0 | 6075.0 | West | Consumer | California | CA | Phones | 0.20 | 16.0110 | 3 | 213.4800 |
# Finding the oldest date in the dataset
import pandas as pd
from datetime import date
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
#Find the oldest date in the dataset
print('---------------------Aggregated min date as a series')
min_date = df.agg({'Order Date': 'min'})
print(type(min_date))
print(min_date)
print('---------------------Get the date as the First item in the series')
min_date = df.agg({'Order Date': 'min'})
min_date = min_date.iat[0]
print(type(min_date))
print(min_date)
print('---------------------Get the date as the First item in the series, date part only')
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
print(type(min_date))
print(min_date)
---------------------Aggregated min date as a series <class 'pandas.core.series.Series'> Order Date 2018-01-03 dtype: datetime64[ns] ---------------------Get the date as the First item in the series <class 'pandas._libs.tslibs.timestamps.Timestamp'> 2018-01-03 00:00:00 ---------------------Get the date as the First item in the series, date part only <class 'datetime.date'> 2018-01-03
# Creating a control for picking a date, using an end date from the data set and today's date
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
#Read the data
df = pd.read_csv("Superstore_Orders_FIPS.csv", parse_dates=["Order Date"])
#Get the oldest date in the datset , date part only
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=min_date,
max_date_allowed=date.today(),
initial_visible_month=date.today(),
date=date.today()
),
html.Div(id='output-container-date-picker-single')
])
@app.callback(
Output('output-container-date-picker-single', 'children'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
#date.fromisoformat(date_string)
#Return a date corresponding to a date_string given in the format YYYY-MM-DD:
date_object = date.fromisoformat(date_value)
#date_string = date_object.strftime('%Y-%m-%d')
date_string = date_object.isoformat() #another format to define date
return string_prefix + date_string
if __name__ == '__main__':
app.run_server(mode='inline', port = 7865)
# Creating a bar graph
import pandas as pd
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
import plotly.express as px
#Read the data
df = pd.read_csv("Superstore_Orders_FIPS.csv", parse_dates=["Order Date"])
#Get the oldest date in the datset , date part only
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=min_date,
max_date_allowed=date.today(),
initial_visible_month=date.today(),
date=date.today()
),
html.Div(id='output-container-date-picker-single')
])
df_aggregated = df.groupby(['Category'], as_index=False).agg({'Sales':'sum'})
fig = px.bar(df_aggregated, x='Category', y='Sales')
fig.show()
@app.callback(
Output('output-container-date-picker-single', 'children'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
#date.fromisoformat(date_string)
#Return a date corresponding to a date_string given in the format YYYY-MM-DD:
date_object = date.fromisoformat(date_value)
#date_string = date_object.strftime('%Y-%m-%d')
date_string = date_object.isoformat() #another format to define date
return string_prefix + date_string
if __name__ == '__main__':
app.run_server(mode='inline', port=8790)
# Creating a dash graph object
import pandas as pd
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
import plotly.express as px
#Read the data
df = pd.read_csv("Superstore_Orders_FIPS.csv", parse_dates=["Order Date"])
#Get the oldest date in the datset , date part only
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=min_date,
max_date_allowed=date.today(),
initial_visible_month=date.today(),
date=date.today()
),
dcc.Graph(id='bar-chart')
])
@app.callback(
Output('bar-chart', 'figure'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
df_aggregated = df.groupby(['Category'], as_index=False).agg({'Sales':'sum'})
fig = px.bar(df_aggregated, x='Category', y='Sales')
return fig
#date.fromisoformat(date_string)
#Return a date corresponding to a date_string given in the format YYYY-MM-DD:
#date_object = date.fromisoformat(date_value)
#date_string = date_object.strftime('%Y-%m-%d')
#date_string = date_object.isoformat() #another format to define date
#return string_prefix + date_string
if __name__ == '__main__':
app.run_server(mode='inline', port=9876)
#Hooking up the graph
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
import plotly.express as px
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
# Get the oldest date in the dataset, date part only
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=min_date,
max_date_allowed=date.today(),
initial_visible_month=date.today(),
date=date.today()
),
dcc.Graph(id='bar-chart')
])
@app.callback(
Output('bar-chart', 'figure'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
start_date = date.fromisoformat(date_value)
end_date= date.today()
#Filter on the date range
df_filter = df[df['Order Date'].isin(pd.date_range(start_date, end_date))]
df_aggregated = df_filter.groupby(['Category'], as_index=False).agg({'Sales':'sum'})
fig = px.bar(df_aggregated, x='Category', y='Sales')
return fig
if __name__ == '__main__':
app.run_server(mode='inline', port=9123)
#Hooking up the graph
from datetime import date, datetime, timedelta
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
import plotly.express as px
import pandas as pd
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
# Get the oldest date in the dataset, date part only
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=min_date,
max_date_allowed=date.today(),
initial_visible_month=date.today() + timedelta(weeks=-8),
date=date.today() + timedelta(weeks=-8)
),
dcc.Graph(id='bar-chart')
])
@app.callback(
Output('bar-chart', 'figure'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
string_prefix = 'You have selected: '
if date_value is not None:
start_date = date.fromisoformat(date_value)
end_date= date.today()
#Filter on the date range
df_filter = df[df['Order Date'].isin(pd.date_range(start_date, end_date))]
df_aggregated = df_filter.groupby(['Category'], as_index=False).agg({'Sales':'sum'})
fig = px.bar(df_aggregated, x='Category', y='Sales')
return fig
if __name__ == '__main__':
app.run_server(mode='inline', port=8121)
#Hooking up the graph
from datetime import date, datetime, timedelta
from datetime import date
import dash
from dash.dependencies import Input, Output
from dash import html
from dash import dcc
from jupyter_dash import JupyterDash
import plotly.express as px
import pandas as pd
#Read the source data
df = pd.read_csv('Superstore_Orders_FIPS.csv', parse_dates=['Order Date'])
# Get the oldest date in the dataset, date part only
min_date = df.agg({'Order Date': 'min'}).dt.date
min_date = min_date.iat[0]
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.DatePickerSingle(
id='my-date-picker-single',
min_date_allowed=min_date,
max_date_allowed=date.today(),
initial_visible_month=date.today() + timedelta(weeks=-8),
date=date.today() + timedelta(weeks=-8)
),
dcc.Graph(id='bar-chart')
])
@app.callback(
Output('bar-chart', 'figure'),
Input('my-date-picker-single', 'date'))
def update_output(date_value):
if date_value is not None:
start_date = date.fromisoformat(date_value)
end_date = date.today()
# Filter on the date range
df_filter = df[df['Order Date'].isin(pd.date_range(start_date, end_date))]
# Group by both Category and Sub-Category
df_aggregated = df_filter.groupby(['Category', 'Sub-Category'], as_index=False).agg({'Sales': 'sum'})
# Create the bar chart with color differentiation for Sub-Category
fig = px.bar(df_aggregated, x='Category', y='Sales', color='Sub-Category')
return fig
if __name__ == '__main__':
app.run_server(mode='inline', port=8867)